
#ifndef _IO_COMM_H
#define _IO_COMM_H

#include "../GenericTypeDefs.h"
#include <stdbool.h>
#include "../xc.h"

#define FRAME_CMD_MIN           (0x40)
#define FRAME_CMD_ReadVersion   (0x41)
#define FRAME_CMD_SetMode       (0x42)
#define FRAME_CMD_EraseFlash    (0x43)
#define FRAME_CMD_ProgramFlash  (0x44)
#define FRAME_CMD_CheckCRC16    (0x45)
#define FRAME_CMD_MAX           (0x46)

#define FRAME_HEAD              (0xAA)
#define FRAME_MASK_CMD          (0x00)
#define FRAME_MASK_ACK          (0x80)
#define FRAME_LISTMASK_CMD      (0x41)
#define FRAME_LISTMASK_ACK      (0xC1)
#define FRAME_ACKLIST_SIZE      (5)

typedef union _IoCommRecPack {
    uint8_t Buf[64 + 5];
    struct {
    uint8_t PackHead;
    uint8_t Cmd;
    uint8_t DataByteLength;
    uint8_t Data[64];
    uint16_t Crc;
    } Members;
} IoCommRecPack;
extern IoCommRecPack IoCommRecPacket;

typedef union _IoCommCmdPack {
    uint8_t Buf[64 + 5];
    struct {
        uint8_t PackHead;
        uint8_t Cmd;
        uint8_t DataByteLength;
        uint8_t Data[64];
        uint16_t Crc;
    } Members;
} IoCommCmdPack;
extern IoCommCmdPack IoCommCmdPacket;

void IoComm_Init(void);
void IoComm_BuildRecPack(void);
bool IoComm_ChkAndClearPackValidFlag(void);
void IoComm_FillTxBytesWithCmdPack(void);
void IoComm_PutByteInTxBuf(uint8_t byte, uint16_t offset);
void IoCommEnterBoot(void);
void IOcmd_test_spi(uint32_t data);

#endif